/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#ifndef avro_ohos_log_H
#define avro_ohos_log_H

#include "hilog/log.h"
#include <cstdio>
#include <napi/native_api.h>
#include <stdexcept>

#define BUFF_SIZE 1024
#define DOUBLE_BUFF_SIZE 2048

#undef LOG_TAG
#define LOG_TAG "ohos_avro"

#define LOGINFO(format, ...)                                                                                           \
    OH_LOG_INFO(LOG_APP, "[%{public}s:%{public}d][%{public}s]" format,                                                 \
                strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__, __LINE__, __func__, ##__VA_ARGS__)

#define LOGWARN(format, ...)                                                                                           \
    OH_LOG_WARN(LOG_APP, "[%{public}s:%{public}d][%{public}s]" format,                                                 \
                strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__, __LINE__, __func__, ##__VA_ARGS__)

#define LOGERROR(format, ...)                                                                                          \
    OH_LOG_ERROR(LOG_APP, "[%{public}s:%{public}d][%{public}s]" format,                                                \
                 strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__, __LINE__, __func__, ##__VA_ARGS__)

#define LOGDEBUG(format, ...)                                                                                          \
    OH_LOG_DEBUG(LOG_APP, "[%{public}s:%{public}d][%{public}s]" format,                                                \
                 strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__, __LINE__, __func__, ##__VA_ARGS__)

#define STATUS_CHECK(status)                                                                                           \
    if (status != napi_ok) {                                                                                           \
        LOGERROR("Fail, status: %{public}d", status);                                                                  \
    }

#define TOBUFFER_STATUS_CHECK(status) \
    do { \
        if ((status) != napi_ok) { \
            throw std::runtime_error( \
                std::string("Fail, File:") + \
                (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__) + \
                ", Line:" + std::to_string(__LINE__) + \
                ", status:" + std::to_string(status) \
            ); \
        } \
    } while (0)

#endif